package com.wkh.project.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wkh.project.aop.ScoringStrategyConfig;
import com.wkh.project.model.dto.question.QuestionContentDto;
import com.wkh.project.model.entity.App;
import com.wkh.project.model.entity.Question;
import com.wkh.project.model.entity.ScoringResult;
import com.wkh.project.model.entity.UserAnswer;
import com.wkh.project.model.vo.QuestionVO;
import com.wkh.project.service.QuestionService;
import com.wkh.project.service.ScoringResultService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @author wangkehua
 * 自定义打分类型评分策略
 */
@ScoringStrategyConfig(appType = 0,scoringStrategy = 0)
public class CustomScoreScoringStrategy implements ScoringStrategy {
    @Resource
    private QuestionService questionService;
    @Resource
    private ScoringResultService scoringResultService;
    @Override
    public UserAnswer doScore(List<String> choice, App app) throws Exception {
        //1.根据appId获得对应应用的题目
        Question question = questionService.getOne(Wrappers.lambdaQuery(Question.class).eq(Question::getAppId, app.getId()));
        //2.根据appId获得对应评分结果的信息列表
        List<ScoringResult> scoringResultList = scoringResultService.list(Wrappers.lambdaQuery(ScoringResult.class)
                .eq(ScoringResult::getAppId, app.getId())
                .orderByDesc(ScoringResult::getResultScoreRange));
        //3.question转换为questionVO
        QuestionVO questionVO = QuestionVO.objToVo(question);
        //4.取出questionContents
        List<QuestionContentDto> questionContents = questionVO.getQuestionContent();
        //5.统计用户得分
        Integer total = 0;
        //遍历题目列表
        for (QuestionContentDto questionContent : questionContents) {
            //遍历用户选择列表
            for (String answer : choice) {
                for (QuestionContentDto.Option option : questionContent.getOptions()) {
                    //如果答案和选项的key匹配
                    if (answer.equals(option.getKey())){
                        //获取选项中的score
                        //如果选项中的score为空，则设置为0
                        int score = Optional.of(option.getScore()).orElse(0);
                        //求和
                        total += score;
                    }
                }
            }
        }
        //6.遍历得分结果，找到用户的得分大于得分范围的第一个，最为最终结果返回
        ScoringResult maxScoringResult = scoringResultList.get(0);
        for (ScoringResult scoringResult : scoringResultList) {
            if (total >= scoringResult.getResultScoreRange()){
                maxScoringResult = scoringResult;
                break;
            }
        }
        //7.返回用户得分结果
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(app.getId());
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choice));
        userAnswer.setResultId(maxScoringResult.getId());
        userAnswer.setResultName(maxScoringResult.getResultName());
        userAnswer.setResultDesc(maxScoringResult.getResultDesc());
        userAnswer.setResultPicture(maxScoringResult.getResultPicture());
        userAnswer.setResultScore(total);
        return userAnswer;
    }
}
